const mysql = require('mysql2/promise');

async function addIdToAreasTable() {
    const connection = await mysql.createConnection({
        host: '43.143.253.188',
        user: 'nextjs',
        password: 'yEAiRE2jcxTyRBy5',
        database: 'nextjs'
    });

    try {
        console.log('🔧 为areas表添加id字段...');

        // 首先检查是否已经有id字段
        const [columns] = await connection.execute('SHOW COLUMNS FROM areas');
        const hasIdField = columns.some(col => col.Field === 'id');

        if (hasIdField) {
            console.log('✅ areas表已经有id字段');
            return;
        }

        // 添加id字段作为主键
        await connection.execute('ALTER TABLE areas ADD COLUMN id INT AUTO_INCREMENT PRIMARY KEY FIRST');
        console.log('✅ 成功为areas表添加id字段作为主键');

        // 验证表结构
        const [newColumns] = await connection.execute('SHOW COLUMNS FROM areas');
        console.log('\n📋 更新后的areas表结构:');
        newColumns.forEach(col => {
            console.log(`  ${col.Field}: ${col.Type} ${col.Null === 'NO' ? 'NOT NULL' : 'NULL'} ${col.Key ? col.Key : ''} ${col.Default ? `DEFAULT ${col.Default}` : ''}`);
        });

    } catch (error) {
        console.error('❌ 添加id字段失败:', error.message);
    } finally {
        await connection.end();
    }
}

addIdToAreasTable();
